//
//  PGStatusCell.swift
//  PGWeibo
//
//  Created by Apple on 15/11/9.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
/// cell控件间距
let statusCellMargin: CGFloat = 12
/// 微博头像大小
let statusIconWidth: CGFloat = 35
class PGStatusCell: UITableViewCell {

    var viewModel: PGStatusViewModel? {
        didSet{
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            
              // 设置配图视图 － 设置视图模型之后，配图视图有能力计算大小
            pictureView.viewModel = viewModel
            //更新约束
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
            }

            
        }
    }
    
    func rowHeight(vm: PGStatusViewModel) -> CGFloat {
        //didSet 方法里能收集到约束更新
        viewModel = vm
        
        //强制布局, 会根据约束算号frame
        contentView.layoutIfNeeded()
        return CGRectGetMaxY(bottomView.frame)
    }
    //构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///顶部用户视图
    private lazy var topView: PGStatusCellTopView = PGStatusCellTopView()
    /// 微博内容视图
     lazy var contentLabel: UILabel = UILabel(title: "这里是正文", screenInset: statusCellMargin)
     lazy var pictureView: PGPictureView = PGPictureView()
    ///底部评论转发视图
     lazy var bottomView: PGStatusCellBottomView = PGStatusCellBottomView()
}

// MARK: - 设置子控件和视图
extension PGStatusCell {
     func setupUI() {
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        //设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(statusCellMargin*2 + statusIconWidth)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
            make.top.equalTo(topView.snp_bottom).offset(statusCellMargin)
        }
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.left.equalTo(contentLabel.snp_left)
//            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
//            make.width.equalTo(200)
//            make.height.equalTo(100)
//        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
        
    }
}